<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NhapDaiLyController
 *
 * @author NghiaPHT
 */
class default_NhapdailyController extends ZF_DefaultController{
    public function indexAction() {
        $this->view->header = "Nhập khách hàng mới";
                
        $model_loaidaily = new Default_Model_Loaidaily();
        $model_daily = new Default_Model_Daily();
        $this->view->loaidaily = $model_loaidaily->listAll();        
        
        if($this->getRequest()->isPost()){
            $madailycha = $this->getRequest()->getParam("dailycha");
            $masophu = $this->getRequest()->getParam("masophu");
            $maloaidaily = $this->getRequest()->getParam("loaidaily");
            $tendaily = $this->getRequest()->getParam("tendaily");
            $ngaysinh = $this->getRequest()->getParam("ngaysinh");
            $cmnd = $this->getRequest()->getParam("cmnd");
            $sdt = $this->getRequest()->getParam("sdt");
            $diachi = $this->getRequest()->getParam("diachi");
            $message = "Nhập đại lý thất bại. Bắt buộc phải điền ngày sinh và số CMND";
            if($madailycha && $maloaidaily && $tendaily && $ngaysinh && $cmnd){
                if($masophu){
                    $madailycha = $masophu;                    
                }
                
                if($this->is_date($ngaysinh)){
                    $dailycha = $model_daily->getById($madailycha);
                    $loaidaily = $model_loaidaily->getById($maloaidaily);
                    if($dailycha && $dailycha != -1 && $loaidaily && $loaidaily != -1){    
                        $arrTemp = explode(" ", $ngaysinh);
                        $arrTemp1 = explode("/", $arrTemp[0]);
                        $arrTemp2 = explode(":", $arrTemp[1]);
                        $ngaysinhSQL = date('YmdHis', mktime($arrTemp2[0], $arrTemp2[1], $arrTemp2[2], $arrTemp1[1], $arrTemp1[0], $arrTemp1[2]));
                        
                        $randNum = (string)(rand(1,9));
                        $madaily = $randNum . date("d") . $arrTemp1[0]  .  date("y") . $arrTemp1[1];                        
                        
                        if(!$this->isExistDaily($madaily)){                        
                            $capdaily = $dailycha['CapDaiLy'] + 1;
                            $ngaygianhap = date("Y-m-d H:i:s");
                            $err = $model_daily->insert(
                                    $madaily, $madailycha, $maloaidaily, $tendaily, 
                                    $ngaysinhSQL, $cmnd, $sdt, $diachi, $ngaygianhap, $capdaily);

                            if($err != -1){   
                                $dsdailycon = $dailycha['DSDaiLyCon'];
                                if($dsdailycon && $dsdailycon != ""){
                                    $dailycha['DSDaiLyCon'] = $dsdailycon . "," . $madaily;
                                }
                                else{
                                    $dailycha['DSDaiLyCon'] = $madaily;
                                }
                                $model_daily->update($madailycha, $dailycha);
                                $message = "Nhập khách hàng thành công. <br><br> Cung cấp cho khách hàng mã số sau để tra cứu khi cần: <b style='color:red'>" .$madaily ."</b>";
                            }
                        }
                    }                
                }
            }
            $this->view->message = $message;
        }
    }
    
    public function is_date( $str ) {
        return true;
        try {
            $dt = new DateTime( trim($str) );
        }
        catch( Exception $e ) {
            return false;
        }
        $month = $dt->format('m');
        $day = $dt->format('d');
        $year = $dt->format('Y');
        if( checkdate($month, $day, $year) ) {
            return true;
        }
        else {
            return false;
        }
    }
    public function isExistDaily($madaily){
        $model_daily = new Default_Model_Daily();
        $daily = $model_daily->getById($madaily);
        if($daily && $daily != -1){
            return true;
        }
        return false;
    }
    
    public function isExistLoaidaily($maloaidaily){
        $model_loaidaily = new Default_Model_Loaidaily();
        $loaidaily = $model_loaidaily->getById($maloaidaily);
        if($loaidaily && $loaidaily != -1){
            return true;
        }
        return false;
    }
    
    public function removeZeros($madaily){        
        return $madaily * 1;
    }    
}

?>
